<?php session_start();
//Check if logged in. If so, send to assignment page.
if(isset($_SESSION['loggedIn'])){
	header('Location: StudentAssignment.php');
}?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link href="main.css" rel="stylesheet" type="text/css" />
<title>Login</title>
<script language="javascript" type="text/javascript" src="../hacs_control/password_control/md5-min.js"></script>
<?php include("../hacs_control/password_control/token_gen.php"); ?>
<script type="text/javascript">
<?php include("../hacs_control/functions/studentPopulate.php");
echo "var array = " . $result . ";\n";
 ?>
 //Checking for enter key
function entSub(e,form){
	var key=e.keyCode || e.which;
	if(key==13){
		submit();
	}
}
function classChanged(i)
{
//When class changed, repopulate the student list to match the class roster.
	document.StudentLoginForm.StudentName.options.length=0;
	document.StudentLoginForm.classCall.value=document.StudentLoginForm.ClassName.options[i].value;
	for(var j = 0; j < array[i].length; j++){
		document.StudentLoginForm.StudentName.options[j] = new Option(array[i][j],array[i][j]);
	}
}
function submit(){
	//Javascript for client to hash the keys to their desired password input
	hash = hex_hmac_md5("<?php echo $salt1 ?>", document.StudentLoginForm.StudentPassword.value);
	hash = hex_hmac_md5("<?php echo $salt2 ?>", hash);
	document.StudentLoginForm.check.value = hash;
	document.StudentLoginForm.student.value=document.StudentLoginForm.StudentName.options[document.StudentLoginForm.StudentName.selectedIndex].value;
	document.login.submit();
}
</script>
</head>
</head>
<body>
&nbsp;
<p>&nbsp; &nbsp;</p>
&nbsp;
	<div id="navigation"><ul>
    <!--<li><a href="logout.php">Logout</a></li>-->
    <!--<li><a href="ChangePassword.php">Change Password</a></li>-->
    <!--<li><a href="StudentAssignment.php">Student Assignments</a></li>-->
    <!--<li><a href="/main/index.php">Schedule</a></li>-->
</ul></div>
    <div id="wrapper">
      	<div id="section">
            <div id='sectionHeader'>Login Page</div>
&nbsp;
        <!-- GROUP CONTENT -->
<p>Select your name from the dropdown list and enter your password</p>
<form action="verify.php" name="StudentLoginForm" id="StudentLoginForm" method="post" onsubmit="javascript: submit()">
<input type="hidden" name="salt1" value="<?php echo $salt1 ?>" />
<input type="hidden" name="salt2" value="<?php echo $salt2 ?>" />
<input type="hidden" name="classCall" value="" />
<input type="hidden" name="student" value="" />
<input type="hidden" name="check" value="" />
<table>
	<tr><td>Class:</td>
	<td><select name="ClassName" onChange="classChanged(document.StudentLoginForm.ClassName.selectedIndex);" style="width: 151px; height: 20px;">
		<?php include("../hacs_control/functions/classPopulate.php") ?>
	</select></td></tr>
	<tr><td>Name:</td>
	<td><select name="StudentName" style="width: 151px; height: 20px;">
	</select></td></tr>
	<script type="text/javascript">
	classChanged(0);
	</script>
	<tr><td>Password:</td> <td><input name="StudentPassword" type="password" onkeypress="entSub(event,this.StudentLoginForm)"/></td></tr>
</table>
</form>
<?php
//Checks session variable to tell if login was incorrect
if(isset($_SESSION['failed'])){
	echo "<br/> Username/Password incorrect";
}
?>
	<!-- END GROUP CONTENT -->
        </div>
    </div>
</body>
</html>